package com.feeyo.location

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.location.LocationListener
import android.location.LocationManager
import android.provider.Settings
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.PermissionUtils.FullCallback
import com.blankj.utilcode.util.Utils
import com.feeyo.groundservice.base_util.SuccessCallback
import com.feeyo.groundservice.base_util.*

/**
 *作者：QLH on 2020-04-23
 *描述：GPS工具类
 */
@SuppressLint("CheckResult")
object GPSUtil {

    private val TAG = javaClass.simpleName

    private var locationListener: LocationListener? = null

    /**
     * 检测是否打开定位权限
     */
    @JvmStatic
    fun isOpenLocationPermission(callback: SuccessCallback) {
        val activity = ActivityUtils.getTopActivity() as AppCompatActivity
        PermissionUtils.permission(Manifest.permission.ACCESS_COARSE_LOCATION)
            .callback(object : FullCallback {
                override fun onGranted(granted: MutableList<String>) {
                    FyLog.e(TAG, "grand:"+granted.toString())
                    if (!isOpenGps()) {
                        activity.showOpenGPSDialog(
                            "您的设备没有打开GPS,是否打开?",
                            Settings.ACTION_LOCATION_SOURCE_SETTINGS
                        )
                    }
                    //获取到定位权限后就开始启动定位，根据提示再手动开启GPS开关
                    callback.success()
                }

                override fun onDenied(
                    deniedForever: MutableList<String>,
                    denied: MutableList<String>
                ) {
                    FyLog.e(TAG, "denied:"+denied.toString())
                    NoPermissionAlert.showDialog(
                        activity,
                        "没有定位的权限，请到系统设置中赋予定位权限"
                    )
                }
            }).request()
    }

    /**
     * 是否打开GPS
     */
    @JvmStatic
    fun isOpenGps(): Boolean {
        val locationManager = ActivityUtils.getTopActivity()
            .getSystemService(Context.LOCATION_SERVICE) as LocationManager
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        val gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
        return gps
    }

    @JvmStatic
    fun setListener(listener: LocationListener) {
        locationListener = listener
    }

    @JvmStatic
    fun gpsLocation() {
        FyLog.e(TAG, "开始GPS定位")
        if (!isOpenGps()) {
            toastCenter("请打开GPS")
            return
        }
        val locationManager = Utils.getApp()
            .getSystemService(Context.LOCATION_SERVICE) as LocationManager
        locationListener?.let {
            locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER, 0, 0f, it
            )
        }
    }
}


fun Activity.showOpenGPSDialog(msg: String, action: String) {
    getConfirmDlg(
        "", msg, null,"去设置", true,
        false, false, confirmListener = object : SuccessCallback {
            override fun success() {
                val intent = Intent(action)
                apply { startActivity(intent) }
            }
        })?.show()


}